package cn.socialcredits.activity.data.repo

import javax.inject.{Inject, Singleton}

import cn.socialcredits.activity.data.driver.MyDriver
import cn.socialcredits.activity.data.implicits.BSONImplicits._
import cn.socialcredits.activity.data.model.Activity
import reactivemongo.bson.{BSONDocument, BSONObjectID}

import scala.concurrent.Future

@Singleton
class ActivityRepo @Inject()(myDriver: MyDriver
                              ) {

  import play.api.libs.concurrent.Execution.Implicits.defaultContext

  def insert(payload: Activity): Future[Activity] = {
    myDriver.collActivity.insert(payload).map { wr =>
      if (wr.n == 1) payload else throw new RuntimeException("insert error: " + payload)
    }
  }

  def findOneById(id: BSONObjectID): Future[Option[Activity]] = {
    myDriver.collActivity.find(BSONDocument("_id" -> id)).one[Activity]
  }
}
